import BuilderDispatch from "../Builder/BuilderDispatch.js";
import FullPathData from "../../eventbus/FullPathData.js";
import FunctionSaveFactory from "./FunctionSaveFactory.js";
let window;
let has_random = []

function getRandom(mapsize){

    let random_where = {
        row: Math.floor(Math.random() * mapsize),
        col: Math.floor(Math.random() * mapsize),
    }
    if(!has_random.includes(random_where)){
        has_random.push(random_where)
        return random_where
    }else{
        return getRandom(mapsize)
    }
}
class SaveManager {
    constructor(win) {
        this.window = win
        this.mapsize = 30
        FullPathData.window = win
    }

    save_data(database, data) {
        this.window.localStorage.setItem(database, JSON.stringify(data))
    }

    load_data(database) {
        return JSON.parse(this.window.localStorage.getItem(database))
    }

    safe_load_data(database) {
        return this.window.localStorage.getItem(database)
    }

    safe_save_data(database, data) {
        this.window.localStorage.setItem(database, data)
    }

    null_map() {
        return {
            resource: null,
            character: null,
            city: null,
            building: null,
            monster: null,
            other: {}
        }
    }

    init() {
        if (this.safe_load_data('data-over')) {
            return
        }
        this.init_player_save()
        this.init_map_data()
        FunctionSaveFactory.FunctionDump( "test", () => {
            console.log("这是test1");
        })

        FunctionSaveFactory.FunctionDump( "test2", () => {
            console.log("这是test2");
        })

    }

    init_map_data() {

        let map_data = Array(this.mapsize).fill(null).map(() =>
            Array(this.mapsize).fill(null).map(() => (this.null_map()))
        );

        console.log(map_data);

        for (let i = 0; i < 50 + this.mapsize - 20; i++) {
            // 0-29的随机数
            let random_number = Math.floor(Math.random() * this.mapsize);
            // 0-29的随机数2
            let random_number2 = Math.floor(Math.random() * this.mapsize);

            console.log(random_number, random_number2, map_data[random_number]);
            // ['stone', 'wood', 'iron'] 随机其中之一
            map_data[random_number][random_number2] = {
                resource: ['stone', 'wood', 'stone', 'wood', 'iron', 'gold'][Math.floor(Math.random() * 6)],
                character: null,
                city: null,
                building: null,
                monster: null,
                other: {}
            };
        }

        this.save_data('map-data', map_data);
        this.safe_save_data('data-over', 'over')
    }
    init_build(id, city){
        this.save_data(`map-builder-${id}`, BuilderDispatch[city.id])
    }

    init_player_save() {
        this.save_data('play', {
            power: "红魔馆"
        })
        this.save_data('power', [
            "红魔馆", "博丽神社", "地灵殿"
        ])
        let mapsize = this.mapsize
        let resources = {
            "红魔馆": {
                wood: 4,
                stone: 2,
                iron: 1,
                gold: 1,
                runpoint: 10,
                build_config: {
                    city: 'type1',
                    master: "红魔馆",
                },
                cities: [{
                    id: 1,
                    ...getRandom(mapsize),
                    master: true
                },{
                    id: 4,
                    ...getRandom(mapsize),
                    master: false
                },
                ] // 所有城市

            },
            "博丽神社": {
                wood: 2,
                stone: 4,
                iron: 1,
                runpoint: 10,
                gold: 1,
                build_config: {
                    city: 'type2',
                    master: "博丽神社",
                },
                cities: [{
                    id: 2,
                    ...getRandom(mapsize),
                    master: false
                }] // 所有城市
            },
            "地灵殿": {
                wood: 6,
                stone: 7,
                iron: 0,
                gold: 0,
                runpoint: 10,
                build_config: {
                    city: 'type3',
                    master: "地灵殿",
                },
                cities: [{
                    id: 3,
                    ...getRandom(mapsize),
                    master: false
                }]// 所有城市
            }
        }
        this.save_data('resource', resources)

        let init_round = 0
        this.save_data('round_wheel', {
            current: 0, // 当前回合
            register: {
                1: {
                    handler: ['test', 'test2']
                }
            } // 回合论效果
        })
    }


}

export default SaveManager;